from tortoise import Tortoise, run_async
from tortoise.exceptions import IntegrityError
import click
import datetime
from config import CONFIG


async def init_db(create_db=False):
    url = 'mysql://{user}:{password}@{host}:{port}/{db_name}?charset=utf8mb4'.format(user=CONFIG.MYSQL_USER, password=CONFIG.MYSQL_PASSWORD,
            host=CONFIG.MYSQL_HOST, port=CONFIG.MYSQL_PORT, db_name=CONFIG.MYSQL_DB_NAME)
    await Tortoise.init(
        db_url=url,
        modules={"models": ["models"]},
        _create_db=create_db
    )


async def init():
    # pass
    # await init_db(create_db=True)
    # await Tortoise._drop_databases()
    await init_db(create_db=True)
    await Tortoise.generate_schemas()


async def migrate():
    await init_db(create_db=False)
    await Tortoise.generate_schemas()



if __name__ == '__main__':
    run_async(migrate())
